package org.dinner.order.web;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.dinner.order.model.DishType;
import org.dinner.order.service.DishTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class DishTypeController {
	@Autowired
	private DishTypeService dishTypeService;

	@RequestMapping(value = "/admin/dishType", method = RequestMethod.GET)
	public String listTypes(Map<String, Object> map) {
		List<DishType> typesList = dishTypeService.list();
		map.put("typesList", typesList);
		
		DishType dishType = new DishType();
		map.put("dishType", dishType);
		
		return "dish_type";
	}
	
	
	@RequestMapping(value = "/admin/dishType", method = RequestMethod.POST)
	public String addContact(@Valid @ModelAttribute("dishType") DishType type,
			BindingResult result) {

		if (result.hasErrors()) {
			return "dish_type";
		}
		
		dishTypeService.add(type);

		return "redirect:/admin/dishType";
	}

}
